package com.yksj.healthtalk.comm;

import android.app.Dialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;

import com.yksj.healthtalk.ui.R;

public class LodingFragmentDialog extends DialogFragment {
	String content;
	/**
	 * @param manager
	 * @param content 文字内容
	 */
	public static LodingFragmentDialog showLodingDialog(FragmentManager manager,String content){
		Fragment fragment = manager.findFragmentByTag("loading");
		FragmentTransaction ft = manager.beginTransaction();
		if(fragment != null){
			ft.remove(fragment);
		}
		LodingFragmentDialog dialog = LodingFragmentDialog.newInstance(content);
		ft.add(dialog,"loading");
		ft.commitAllowingStateLoss();
		return dialog;
	}
	
	/**
	 * 默认的文字提示
	 * @param manager
	 * @param resources
	 */
	public static LodingFragmentDialog showLodingDialog(FragmentManager manager,Resources resources){
		String content = resources.getString(R.string.loading);
		return showLodingDialog(manager, content);
	}
	
	public static void dismiss(FragmentManager fragmentManager){
		if(fragmentManager == null)return;
		DialogFragment fragment = (DialogFragment)fragmentManager.findFragmentByTag("loading");
		if(fragment != null)fragment.dismissAllowingStateLoss();
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = new Dialog(getActivity(),R.style.dialog);
		dialog.setContentView(R.layout.loading_dialog_layout);
		TextView textView = (TextView)dialog.findViewById(R.id.loadingTxt);
		textView.setText(content);
		return dialog;
	}
	
	public boolean isShowing(){
		return getDialog().isShowing();
	}
	
	static LodingFragmentDialog newInstance(String content){
		LodingFragmentDialog dialog = new LodingFragmentDialog();
		Bundle bundle = new Bundle();
		bundle.putString("content",content);
		dialog.setArguments(bundle);
		return dialog;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		content = getArguments().getString("content");
	}
}
